package oj;


import java.util.*;

/**
 * oj题 232：用栈实现队列
 * 请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作（push、pop、peek、empty）：
 * 实现 MyQueue 类：
 * void push(int x) 将元素 x 推到队列的末尾
 * int pop() 从队列的开头移除并返回元素
 * int peek() 返回队列开头的元素
 * boolean empty() 如果队列为空，返回 true ；否则，返回 false
 */
public class MyQueue {
    private final Deque<Integer> stack1 = new LinkedList<>();
    private final Deque<Integer> stack2 = new LinkedList<>();
    public MyQueue() {

    }

    public void push(int x) {
        stack1.push(x);
    }

    public int pop() {
        if(empty()){
            return -1;
        }
        if(stack2.isEmpty()){
            while(!stack1.isEmpty()){
                int e = stack1.pop();
                stack2.push(e);
            }
        }
        return stack2.pop();
    }

    public int peek() {
        if(empty()){
            return -1;
        }
        if(stack2.isEmpty()){
            while(!stack1.isEmpty()){
                int e = stack1.pop();
                stack2.push(e);
            }
        }
        return stack2.peek();
    }

    public boolean empty() {
        return stack1.isEmpty() && stack2.isEmpty();
    }

    public static void main(String[] args) {
        MyQueue queue = new MyQueue();
        queue.push(1);
        queue.push(2);
        queue.push(3);
        queue.push(4);
        queue.push(5);
        System.out.println(queue.peek());       //1
        queue.pop();
        System.out.println(queue.peek());       //2
        queue.push(6);
        queue.push(7);
        queue.pop();
        System.out.println(queue.peek());       //3
        System.out.println(queue.pop());        //3
        System.out.println(queue.pop());        //4
        System.out.println(queue.pop());        //5
        System.out.println(queue.pop());        //6
        System.out.println(queue.pop());        //7
    }
}
